﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.ServiceModel;

namespace ReadeAssetManager
{
    public partial class ucRegister : UserControl
    {
        private AMSServiceRef.User _user;
        private delegateReturn _returnaction;

        private const string STR_SUCCESS = "Registration Successful!";
        private const string STR_FAIL = "Registration Failed.  Please make sure you entered a user name and " +
                                        "that the user name you selected does not already exist.";

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="ReturnAction">[delegateReturn] Delegate method called on frmMain to inform
        /// the main form that an event related to registration has occurred.</param>
        public ucRegister(delegateReturn ReturnAction)
        {
            InitializeComponent();
            _returnaction = ReturnAction;
        }

        /// <summary>
        /// CurrentUser
        /// Property getter to expose the currently logged in user (unless null) to frmMain.
        /// </summary>
        public AMSServiceRef.User CurrentUser
        {
            get { return _user; }
        }

        /// <summary>
        /// btnRegister_Click
        /// Initiates a register request with the WCF service.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnRegister_Click(object sender, EventArgs e)
        {
            if (Register())
            {
                lblRegisterResult.Text = STR_SUCCESS;
                _returnaction(this);
            }
            else
            {
                lblRegisterResult.Text = STR_FAIL;
                _returnaction(this);
            }
        }

        /// <summary>
        /// Register
        /// Invokes the Register method of the WCF service.
        /// </summary>
        /// <returns>[bool] True if the registration request was successful, otherwise false.</returns>
        private bool Register()
        {
            AMSServiceRef.AMSServiceClient client = new ReadeAssetManager.AMSServiceRef.AMSServiceClient();
            AMSServiceRef.User user = new AMSServiceRef.User();
            user.Name = txtUserName.Text;
            user.Password = txtPassword.Text;
            try
            {
                if (client.Register(user))
                {
                    _user = user;
                    return true;
                }
                else
                    return false;
            }
            catch (CommunicationException cex)
            {
                MessageBox.Show("Communication Error: " + cex.Message + "\n" + cex.InnerException.ToString());
                return false;
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error: " + ex.Message);
                return false;
            }
        }
    }
}
